<?php
include APPLICATION_PATH."/models/base/FeedbackBase.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Feedback extends Model_FeedbackBase{   
   
    
}
class Model_FeedbackMapper extends Model_FeedbackMapperBase{
   public function fetchAllP(){
        $db = Zend_Db_Table::getDefaultAdapter();
       $select="select * from  feedback where title not like 'TB%' order by modified DESC";  
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Feedback();      
            $entry->setId($row->id)
                ->setId($row->id)                
                ->setUser_Id($row->user_id)
                ->setQuestion($row->question)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setAdmin_Id($row->admin_id)   
                ->setAnswer($row->answer)
                ->setTitle($row->title);
            $entries[] = $entry;
        }
        return $entries;
    }
    //list cau thong bao chua doc
    public function fetchAllTB(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select='select * from  feedback where question !="" order by modified DESC';        
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Feedback();      
            $entry->setId($row->id)
                ->setId($row->id)                
                ->setUser_Id($row->user_id)
                ->setQuestion($row->question)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setAdmin_Id($row->admin_id)   
                ->setAnswer($row->answer)
                ->setTitle($row->title);
            $entries[] = $entry;
        }
        return $entries;
    }
    //list cau thong bao doc roi
    public function fetchAllTBL($title,$where,$orderby){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select="select id,user_id,question,created,created_by,modified,modified_by,admin_id,answer,".$title." as title from  feedback where ".$where." order by ".$orderby."";        
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Feedback();      
            $entry->setId($row->id)
                ->setId($row->id)                
                ->setUser_Id($row->user_id)
                ->setQuestion($row->question)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setAdmin_Id($row->admin_id)   
                ->setAnswer($row->answer)
                ->setTitle($row->title);
            $entries[] = $entry;
        }
        return $entries;
    }
    //
    
    public static function getFeedback(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select="Select * From feedback where answer!='' and title= 'FEEDBACK' ORDER BY created DESC limit 0,100";        
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Feedback();      
            $entry->setId($row->id)
                ->setId($row->id)                
                ->setUser_Id($row->user_id)
                ->setQuestion($row->question)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setAdmin_Id($row->admin_id)   
                ->setAnswer($row->answer)
                ->setTitle($row->title);
            $entries[] = $entry;
        }
        return $entries;
    }
     public static function getFeedbackUser($user_id){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select="Select * From feedback where title= 'FEEDBACK' and user_id='$user_id' ORDER BY created DESC limit 0,100";        
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Feedback();      
            $entry->setId($row->id)
                ->setId($row->id)                
                ->setUser_Id($row->user_id)
                ->setQuestion($row->question)
                ->setCreated($row->created)
                ->setCreated_By($row->created_by)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by)
                ->setAdmin_Id($row->admin_id)   
                ->setAnswer($row->answer)
                ->setTitle($row->title);
            $entries[] = $entry;
        }
        return $entries;
    }
    
    public static function getFeedbackKinhDoanh($kinhdoanh_id){
        $db=Zend_Db_Table::getDefaultAdapter();
        $select= "Select feedback.id as id, IFNULL(user.id,0) as user_id,"
                . " IFNULL(feedback.question,'') as question, IFNULL(feedback.answer) as answer"
                . " From feedback left join user on feedback.user_id = user.id"
                . " left join agent on user.id = agent.user_id"
                . " left join admin_area on agent.province_id = admin_area.admin_id"
                . " Where admin_area.admin_id='$kinhdoanh_id'";
    }
}

